package game.bolita.components;

import java.awt.Color;
import java.util.Random;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.GameComponent;
import com.uqbar.vainilla.GameScene;
import com.uqbar.vainilla.appearances.Circle;

public class Ball extends GameComponent<GameScene> {

	private int speed    = 120;
	private int diameter = 20;
	private Color color  = Color.green;
	private double posicionI, posicionJ;
	
	public Ball() {		
		this.setAppearance(new Circle(this.getColor(), this.getDiameter()));
	}
	
	@Override
	public void onSceneActivated() {
		Random random = new Random();
		
		this.setX(random.nextDouble() * this.getGame().getDisplayWidth());	//Posicion X inicial al azar
		this.setY(random.nextDouble() * this.getGame().getDisplayHeight());	//Posicion Y inicial al azar
		this.setPosicionI(random.nextDouble() * 2 - 1); //Calculo para que la bolita se mueva en horizontal y hacia alguna direccion al azar
		this.setPosicionJ(random.nextDouble() * 2 - 1); //Calculo para que la bolita se mueva en vertical y hacia alguna direccion al azar

		double modulo = Math.sqrt(this.getPosicionI() * this.getPosicionI() + this.getPosicionJ() * this.getPosicionJ());

		this.setPosicionI(this.getPosicionI() / modulo); //Versor I
		this.setPosicionJ(this.getPosicionJ() / modulo); //Versor J

		super.onSceneActivated();
	}
	
	@Override
	public void update(DeltaState deltaState) {
		double advanced = deltaState.getDelta() * this.getSpeed();
		this.move(this.getPosicionI() * advanced, this.getPosicionJ() * advanced);		

		double width  = this.getGame().getDisplayWidth();
		double height = this.getGame().getDisplayHeight();
		
		if (this.getY() > width - this.getDiameter()) {
			this.setY(width - this.getDiameter());
			this.setPosicionJ(-this.getPosicionJ());
		}
		
		if (this.getX() > height - this.getDiameter()) {
			this.setX(height - this.getDiameter());
			this.setPosicionI(-this.getPosicionI());
		}
		
		if (this.getY() < 0) {
			this.setY(0);
			this.setPosicionJ(-this.getPosicionJ());
		}
			
		if (this.getX() < 0) {
			this.setX(0);
			this.setPosicionI(-this.getPosicionI());
		}
		
		super.update(deltaState);
	}

//	private boolean chocaDeFormaHorizontal() {
//		return this.getX() > this.getGame().getDisplayWidth() - this.getDiameter() || this.getX() < 0;
//	}
//	private boolean chocaDeFormaVertical() {
//		return this.getY() > this.getGame().getDisplayHeight() - this.getDiameter() || this.getY() < 0;
//	}
	
	
	
	// ************************************************************
	// ** ACCESSORS
	// ************************************************************
	
	public int getSpeed() {
		return speed;
	}
	public int getDiameter() {
		return diameter;
	}
	public Color getColor() {
		return color;
	}	
	public double getPosicionI() {
		return posicionI;
	}
	public void setPosicionI(double posicionI) {
		this.posicionI = posicionI;
	}
	public double getPosicionJ() {
		return posicionJ;
	}
	public void setPosicionJ(double posicionJ) {
		this.posicionJ = posicionJ;
	}
	public void setSpeed(int speed) {
		this.speed = speed;
	}
	
}